<!DOCTYPE html>
<html lang="en">

<head>
    <style>
        .box {
            display: flex;
            justify-content: center;
            text-align: center;
            margin-top: 20px;
        }

        p {
            width: 100px;
            height: 100px;
            background-color: #000;
            margin-left: 20px;
        }

        .big {
            display: flex;
            justify-content: center;
            text-align: center;
        }

        button {
            width: 60px;
            margin: auto;
            margin-top: 20px;
        }

        .bgc {
            background: red;
        }

        * {
            margin: 0;
            padding: 0;
        }
    </style>
</head>

<body>
    <div class="big"><button>改变颜色</button></div>
    <div class="box">

        <p></p>
        <p></p>
        <p></p>
    </div>
</body>
<script>
    var but = document.getElementsByTagName('button')[0]
    var p = document.getElementsByTagName('p')
    // console.log(p);
    but.onclick = function () {
        // console.log(1);
        for (var i = 0; i < p.length; i++) {
            p[i].className = 'bgc'
        }
    }




</script>

</html>